/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-11-24
 * Time:18:45
 */
public class PriorityQueue {
    public int[] elem;
    public int usedSize;

    public PriorityQueue() {
        elem=new int[10];
    }

    /**
     * 建堆的时间复杂度：
     *
     * @param array
     */
    public void createHeap(int[] array) {
        int parent;
        int child;
        for (int i = 0; i < array.length; i++) {
            parent=i;
            child=2*parent+1;
            while(child<array.length){
                if(array[child]<array[child+1]){
                    child=child+1;
                }
                if(array[child]>array[parent]){
                    int tmp=array[parent];
                    array[parent]=array[child];
                    array[child]=tmp;
                }
                parent=child;
                child=parent*2+1;
            }
        }
        for (int i = 0; i < array.length; i++) {
            elem[i]=array[i];
            usedSize++;
        }
    }

    /**
     *
     * @param root 是每棵子树的根节点的下标
     * @param len  是每棵子树调整结束的结束条件
     * 向下调整的时间复杂度：O(logn)
     */
    private void shiftDown(int root,int len) {
        int parent;
        int child;
        for (int i = root; i < len;i++) {
            parent=i;
            child=2*parent+1;
            while(child<len){
                if(child+1<usedSize&&elem[child]<elem[child+1]){
                    child=child+1;
                }
                if(elem[child]>elem[parent]){
                    int tmp=elem[parent];
                    elem[parent]=elem[child];
                    elem[child]=tmp;
                }
                parent=child;
                child=parent*2+1;
            }
        }
    }


    /**
     * 入队：仍然要保持是大根堆
     * @param val
     */
    public void push(int val) {
        if(isFull()){
            System.out.println("满了");
            return;
        }
        elem[usedSize++]=val;
        shiftUp(usedSize-1);
    }

    private void shiftUp(int child) {
        while(child>0){
            int parent=(child-1)/2;
            if(child%2==0){
                if(elem[child]<elem[child-1]){
                    child=child-1;
                }
                if(elem[child]>elem[parent]){
                    int tmp=elem[parent];
                    elem[parent]=elem[child];
                    elem[child]=tmp;
                }
            }else{
                if(elem[child]<elem[child+1]){
                    child=child+1;
                }
                if(elem[child]>elem[parent]){
                    int tmp=elem[parent];
                    elem[parent]=elem[child];
                    elem[child]=tmp;
                }
            }
            child=parent;
        }
    }

    public boolean isFull() {
        return usedSize == elem.length;
    }

    /**
     * 出队【删除】：每次删除的都是优先级高的元素
     * 仍然要保持是大根堆
     */
    public void pollHeap() {
        if(isEmpty()){
            System.out.println("空的");
            return;
        }
        if(usedSize==1){
            usedSize--;
        }
        int tmp=elem[usedSize-1];
        elem[usedSize-1]=elem[0];
        elem[0]=tmp;
        usedSize--;
        shiftDown(0,usedSize);
    }

    public boolean isEmpty() {
        return usedSize == 0;
    }

    /**
     * 获取堆顶元素
     * @return
     */
    public int peekHeap() {
        if(isEmpty()){
            System.out.println("没有这个数");
            return -1;
        }
        return elem[0];
    }
}
